package com.wut.clock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wut.clock.common.constant.CommentConstant;
import com.wut.clock.common.exception.Asserts;
import com.wut.clock.common.util.PageUtil;
import com.wut.clock.entity.Comment;
import com.wut.clock.mapper.CommentMapper;
import com.wut.clock.service.ICommentService;
import com.wut.clock.vo.admin.CommentVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yuwei
 * @Date: 2022/6/1 13:18
 * @Description:
 */

@Service
@RequiredArgsConstructor
@SuppressWarnings("unchecked")
public class CommentServiceImpl implements ICommentService {

    private final CommentMapper commentMapper;

    @Transactional
    @Override
    public void deleteComment(Integer commentId) {
        LambdaQueryWrapper<Comment> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(Comment::getTopicType,CommentConstant.TopicType.COMMENT.getCode()).eq(Comment::getTopicId,commentId);
        List<Comment> comments = commentMapper.selectList(deleteWrapper);
        if (CollectionUtils.isNotEmpty(comments)) {
            for (Comment comment : comments) {
                deleteComment(comment.getId());
            }
        }
        commentMapper.deleteById(commentId);
    }


    @Override
    public List<CommentVo> getCommentTreeList(Integer pageNum, Integer pageSize, String nickname, Integer status) {
        List<CommentVo> commentVos = commentMapper.getCommentList(nickname,status);
        List<CommentVo> commentVoList = buildCommentTree(commentVos);
        return PageUtil.startPage(commentVoList,pageNum,pageSize);
    }

    @Transactional
    @Override
    public void updateCommentsReview(Integer commentId) {
        Comment comment = commentMapper.selectById(commentId);
        if (CommentConstant.Status.AVAILABLE.getCode().equals(comment.getStatus())) {
            Asserts.fail("操作失败，该条评论状态正常");
        }
        comment.setStatus(CommentConstant.Status.AVAILABLE.getCode());
        commentMapper.updateById(comment);
    }

    private List<CommentVo> buildCommentTree(List<CommentVo> commentVos) {
        List<CommentVo> res = commentVos.stream()
                .filter(commentVo -> commentVo.getTopicType().equals(CommentConstant.TopicType.ARTICLE.getCode()))
                .sorted(Comparator.comparing(Comment::getScore))
                .map(commentVo -> convert(commentVo, commentVos))
                .collect(Collectors.toList());
        return res;
    }

    private CommentVo convert(CommentVo commentVo,List<CommentVo> commentVos) {
        List<CommentVo> children = commentVos.stream()
                .filter(subComment -> subComment.getTopicType().equals(CommentConstant.TopicType.COMMENT.getCode())
                        && subComment.getTopicId().equals(commentVo.getId()))
                .map(subComment -> convert(subComment, commentVos))
                .collect(Collectors.toList());
        commentVo.setChildren(children);
        return commentVo;
    }
}
